package com.hanthink.web.controller.pc;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.hanthink.common.annotation.Log;
import com.hanthink.common.core.controller.BaseController;
import com.hanthink.common.core.domain.AjaxResult;
import com.hanthink.common.core.domain.model.LoginUser;
import com.hanthink.common.core.page.TableDataInfo;
import com.hanthink.common.enums.BusinessType;
import com.hanthink.common.utils.ServletUtils;
import com.hanthink.common.utils.poi.ExcelUtil;
import com.hanthink.framework.web.service.TokenService;
import com.hanthink.system.domain.pc.EmItemFile;
import com.hanthink.system.service.pc.IEmItemFileService;

/**
 * 设备点检文件Controller
 * 
 * @author hanthink
 * @date 2021-01-27
 */
@RestController
@RequestMapping("/equipment/file")
public class EmItemFileController extends BaseController
{
    @Autowired
    private IEmItemFileService emItemFileService;
    
	@Autowired
	private TokenService tokenService;

    /**
     * 查询设备点检文件列表
     */
    @PreAuthorize("@ss.hasPermi('equipment:file:list')")
    @GetMapping("/list")
    public TableDataInfo list(EmItemFile emItemFile)
    {
        startPage();
        List<EmItemFile> list = emItemFileService.selectEmItemFileList(emItemFile);
        return getDataTable(list);
    }

	@Log(title = "设备点检子项", businessType = BusinessType.IMPORT)
	@PreAuthorize("@ss.hasPermi('equipment:file:import')")
	@PostMapping("/importData")
	public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception {
		ExcelUtil<EmItemFile> util = new ExcelUtil<EmItemFile>(EmItemFile.class);
		List<EmItemFile> itemList = util.importExcel(file.getInputStream());
		LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
		String operName = loginUser.getUsername();
		String message = emItemFileService.importemItemFile(itemList, updateSupport, operName);
		return AjaxResult.success(message);
	}

	@GetMapping("/importTemplate")
	public AjaxResult importTemplate() {
		ExcelUtil<EmItemFile> util = new ExcelUtil<EmItemFile>(EmItemFile.class);
		return util.importTemplateExcel("设备点检子项数据");
	}
	
    /**
     * 导出设备点检文件列表
     */
    @PreAuthorize("@ss.hasPermi('equipment:file:export')")
    @Log(title = "设备点检文件", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(EmItemFile emItemFile)
    {
        List<EmItemFile> list = emItemFileService.selectEmItemFileList(emItemFile);
        ExcelUtil<EmItemFile> util = new ExcelUtil<EmItemFile>(EmItemFile.class);
        return util.exportExcel(list, "file");
    }

    /**
     * 获取设备点检文件详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(emItemFileService.selectEmItemFileById(id));
    }

    /**
     * 新增设备点检文件
     */
    @PreAuthorize("@ss.hasPermi('equipment:file:add')")
    @Log(title = "设备点检文件", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody EmItemFile emItemFile)
    {
        return toAjax(emItemFileService.insertEmItemFile(emItemFile));
    }

    /**
     * 修改设备点检文件
     */
    @PreAuthorize("@ss.hasPermi('equipment:file:edit')")
    @Log(title = "设备点检文件", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody EmItemFile emItemFile)
    {
        return toAjax(emItemFileService.updateEmItemFile(emItemFile));
    }

    /**
     * 删除设备点检文件
     */
    @PreAuthorize("@ss.hasPermi('equipment:file:remove')")
    @Log(title = "设备点检文件", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(emItemFileService.deleteEmItemFileByIds(ids));
    }
}
